<?php

namespace app\api\controller;

use app\common\BaseHelper;

use app\admin\model\Userprojecttask as UserprojecttaskMdl;
use app\admin\model\Userproject;
use app\admin\model\User;
use app\admin\model\Member;
use app\admin\model\Image;

use think\Db;
use think\Exception;
use think\Request;

class Userprojecttask extends BaseController
{

    /**
     * 列表
     */
    public function getTasks()
    {
        $param = self::getHttpParam();

        $start = 0;
        $length = 20;

        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }
        if (empty($param->projectId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'projectId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $projectId = $param->projectId;
        $where = [
            'projectId' => $projectId
        ];

        $model = new UserprojecttaskMdl();
        $fields = '*';
        $order = 'createDate DESC';
        $result = $model->where($where)->field($fields)->limit($start, $length)->order($order)->select();
        $total = $model->where($where)->count();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关数据');
        }

        foreach ($result as $item) {
            // 执行人信息
            $executorInfo = User::where(['id' => $item['executor']])->field('memberId, nickName as name, avatarUrl as avatar')->find();
            if($executorInfo['memberId']){
                $executorInfo = Member::where(['id' => $executorInfo['memberId']])->field('name, avatar')->find();
            }
            $item['executorInfo'] = $executorInfo;
            // 项目信息
            $projectInfo = Userproject::where(['id' => $item['projectId']])->field('userId, name')->find();
            $item['projectInfo'] = $projectInfo;
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 详情
     */
    public function getTaskDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->taskId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'taskId 不能为空');
        }

        $where = [
            'id' => $param->taskId
        ];
        $model = new UserprojecttaskMdl();
        $field = '*';
        $item = $model->where($where)->field($field)->find();
        if (!$item) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到相关数据');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
    }

    /**
     * 保存
     */
    public function saveTask()
    {
        $param = self::getHttpParam();

        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }
        if (empty($param->projectId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '项目ID(projectId) 不能为空');
        }
        if (empty($param->title)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '标题(title) 不能为空');
        }
        if (empty($param->describe)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '描述(describe) 不能为空');
        }
        if (empty($param->deadline)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '截止时间(deadline) 不能为空');
        }
        if (empty($param->remind)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '提醒时间(remind) 不能为空');
        }
        if (empty($param->executor)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '执行人(executor) 不能为空');
        }

        $userId = $param->userId;

        Db::startTrans();
        try {
            $data = (array)$param;
            $data['deadline'] = strtotime($data['deadline']);
            $model = new UserprojecttaskMdl();

            if (!empty($param->id)) {
                $id = $param->id;
                $data['updateDate'] = time();
                $data['userId'] = $userId;

                $result = $model->allowField(true)->save($data, ['id' => $id]);
            } else {
                $data['id'] = BaseHelper::getUUID();
                $data['createDate'] = time();

                $result = $model->save($data);
            }

            if($result !== false){
                Db::commit();
            }else{
                Db::rollback();
                return show(config('status.ERROR_STATUS'), '保存失败', $model->getError());
            }
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), '保存失败', $e->getMessage());
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $data);
    }

    /**
     * 删除
     */
    public function delTask()
    {
        $param = self::getHttpParam();

        if (empty($param->taskId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'taskId 不能为空');
        }

        $where = [
            'id' => $param->taskId
        ];
        $model = new UserprojecttaskMdl();
        $result = $model->where($where)->delete();
        if ($result === false) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '删除失败');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

}